package umg.gui;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import umg.Constants;
import umg.util.DBUtil;
import umg.util.PropertyManager;

public class CustomTableCellRenderer extends DefaultTableCellRenderer
{
	private static final long serialVersionUID = -7391408481383244118L;
	private static DBUtil db;

	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
	{
		Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		String[] ipWatches, nameWatches, adminIPAddresses;

		if (value == null) return cell;//nothing we can do.

		if (value instanceof String)//0,4,5 are special (name,ip,team)
		{
			String strvalue = (String) value;
			//System.out.println("is a String. col="+column);

			ipWatches = PropertyManager.getIPWatchListArray();
			nameWatches = PropertyManager.getNameWatchListArray();
			adminIPAddresses = PropertyManager.getAdminIPAddressesArray();

			if (column == 0)// name
			{
				if (db == null) db = new DBUtil();

				setHorizontalAlignment(LEFT);
				if (strvalue != null && (strvalue.toLowerCase().indexOf("admin") != -1))
				{
					setForeground(Color.RED);
				}
				else if (strvalue != null && strvalue.endsWith("�"))// bot sign
				{
					setForeground(Color.MAGENTA);
				}
				else
				{
					boolean watched = false;
					// check if a name_Watch exists for this name
					// Loop through all the watches, check if the current name
					// matches/contains it
					for (int i = 0; i < nameWatches.length; i++)
					{
						if (nameWatches[i].length() > 0 && strvalue.toLowerCase().indexOf(nameWatches[i].toLowerCase()) != -1)
						{
							// found a watched name, change color to red+gray
							setForeground(new Color(172, 23, 23));
							watched = true;
							break;
						}
					}
					if (!watched) setForeground(Color.WHITE);
				}
			}
			else if (column == 5)// team
			{
				setHorizontalAlignment(CENTER);
				this.setSize(10, 15);
				if (value == null || (value instanceof String == false)) return cell;
				if (strvalue.startsWith(Constants.BLUE_STR))
				{
					// blue team
					// cell.setBackground( Color.blue );
					cell.setForeground(Color.CYAN);
				}
				else if (strvalue.startsWith(Constants.RED_STR))
				{
					// red team
					// cell.setBackground( Color.red );
					cell.setForeground(Color.RED);
				}
				else if (strvalue.startsWith(Constants.SPEC_STR))
				{
					// red team
					// cell.setBackground( Color.yellow );
					cell.setForeground(Color.YELLOW);
				}
				else
				{
					cell.setForeground(Color.WHITE);
				}
				// System.out.println("Value="+value);
			}
			else if (column == 4)// ip
			{
				// removed: setToolTipText();//because we have a dedicated Flag/Tooltip
				if (strvalue != null && db.doesIPHaveAliases(strvalue) == true)// has aliases
				{
					setForeground(Color.GRAY);
				}
				else
				{
					setForeground(Color.WHITE);
				}
				// Loop through all the watches, check if the current IP
				// matches/contains it
				for (int i = 0; i < ipWatches.length; i++)
				{
					if (ipWatches[i].length() > 0 && strvalue.startsWith(ipWatches[i]) == true)
					{
						// found a watched IP, change color to red+gray
						setForeground(new Color(172, 23, 23));
						break;
					}
				}

				// Loop through the admin IP list
				for (int i = 0; i < adminIPAddresses.length; i++)
				{
					if (adminIPAddresses[i].length() > 0 && strvalue.startsWith(adminIPAddresses[i]) == true)
					{
						setForeground(Color.GREEN);
						break;
					}
				}
			}
			else
			// all other columns
			{
				setHorizontalAlignment(CENTER);
				setForeground(Color.WHITE);
				setBackground(new Color(36, 57, 84));
			}
		}//end if value is a String

		return cell;
	}

	// The following methods override the defaults for performance reasons
	public void validate()
	{
	}

	public void revalidate()
	{
	}

	protected void firePropertyChange(String propertyName, Object oldValue, Object newValue)
	{
	}

	public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue)
	{
	}
}
